package com.open.cloud.rbac.controller.application;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.inspur.micro.core.response.ResponseWrapper;
import com.open.cloud.rbac.entity.application.CoreApplication;
import com.open.cloud.rbac.object.application.CoreApplicationDto;
import com.open.cloud.rbac.object.application.CoreApplicationSelectDto;
import com.open.cloud.rbac.object.application.CoreApplicationSyncDto;
import com.open.cloud.rbac.service.application.ICoreApplicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 系统应用表 前端控制器
 * </p>
 *
 * @author wangzengzhou01
 * @since 2024-12-23
 */
@Api(tags = "RBAC服务-应用业务")
@Slf4j
@RestController
@RequestMapping(value = "api/v1/core/application")
public class CoreApplicationController {

    @Resource
    private ICoreApplicationService applicationService;

    @ApiOperation(value = "创建应用")
    @PostMapping("/add")
    public ResponseWrapper addApplication(@RequestBody CoreApplicationDto obj) {
        JSONObject result = applicationService.addApplicationInfo(obj);
        return result.getInteger("code") == 200 ? ResponseWrapper.success() : ResponseWrapper.failure(result.getInteger("code"), result.getString("message"));
    }

    @ApiOperation(value = "修改应用")
    @PostMapping("/update")
    public ResponseWrapper updateApplication(@RequestBody CoreApplicationDto obj) {
        JSONObject result = applicationService.updateApplicationInfo(obj);
        return result.getInteger("code") == 200 ? ResponseWrapper.success() : ResponseWrapper.failure(result.getInteger("code"), result.getString("message"));
    }

    @ApiOperation(value = "按ID删除")
    @GetMapping("/delete")
    public ResponseWrapper deleteApplication(@RequestParam(name = "id") String id) {
        JSONObject result = applicationService.deleteApplicationById(id);
        return result.getInteger("code") == 200 ? ResponseWrapper.success() : ResponseWrapper.failure(result.getInteger("code"), result.getString("message"));
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResponseWrapper selectPage(@RequestBody CoreApplicationSelectDto applicationSelectDto) {
        IPage<CoreApplication> data = applicationService.selectApplicationByPage(applicationSelectDto);
        return ResponseWrapper.success(data);
    }

    @PostMapping("/sync")
    @ApiOperation(value = "接口同步")
    public ResponseWrapper appInterfaceSync(@RequestBody CoreApplicationSyncDto appSyncDto) {
        JSONObject result = applicationService.syncAppInterface(appSyncDto);
        String message = result.getString("message");
        return result.getInteger("code") == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(result.getInteger("code"), message);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询列表")
    public ResponseWrapper getAppList(@RequestBody CoreApplicationSelectDto applicationSelectDto) {
        List<CoreApplication> data = applicationService.getApplicationList(applicationSelectDto);
        return ResponseWrapper.success(data);
    }

}

